import java.util.concurrent.atomic.AtomicInteger;

public class CASDemo {
    private static AtomicInteger count= new AtomicInteger(0);
    public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()-> {
            for(int i=0;i<50000;i++) {
//                count++;
                count.getAndIncrement();//Java中运算符不能重载,只能手动调用方法
//                count.incrementAndGet();
//                count.decrementAndGet();
//                count.getAndDecrement();
            }
        });
        Thread t2=new Thread(()-> {
            for(int i=0;i<50000;i++) {
//                count++;
                count.getAndIncrement();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        //通过count.get拿到原子类内部持有的真实数据
        System.out.println("count="+count.get());
    }
}
